package xyz.ssf.cloud.project.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xyz.ssf.cloud.common.core.domain.PageResult;
import xyz.ssf.cloud.project.system.domain.LoginLogQuery;
import xyz.ssf.cloud.project.system.domain.SysLoginLog;
import xyz.ssf.cloud.project.system.mapper.SysLoginLogMapper;
import xyz.ssf.cloud.project.system.service.ISysLoginLogService;

/**
 * 系统访问日志情况信息 服务层处理
 * 
 * @author ssf
 */
@Service
public class SysLoginLogServiceImpl implements ISysLoginLogService {

    @Autowired
    private SysLoginLogMapper loginLogMapper;

    /**
     * 新增系统登录日志
     * 
     * @param loginLog 访问日志对象
     * @return 结果
     */
    @Override
    public int insertLoginLog(SysLoginLog loginLog) {
        loginLogMapper.insertLoginLog(loginLog);
        return 1; // 插入成功返回1
    }

    /**
     * 查询系统登录日志集合
     * 
     * @param loginLog 登录日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLoginLog> selectLoginLogList(SysLoginLog loginLog) {
        return loginLogMapper.selectLoginLogList(loginLog);
    }
    
    /**
     * 分页查询系统登录日志
     * 
     * @param query 查询参数
     * @return 分页结果
     */
    @Override
    public PageResult<SysLoginLog> selectLoginLogPage(LoginLogQuery query) {
        SysLoginLog loginLog = new SysLoginLog();
        loginLog.setUsername(query.getLoginName());
        loginLog.setIpAddr(query.getIpAddress());
        loginLog.setStatus(query.getLoginStatus());
        
        // 处理时间范围查询
        Map<String, Object> params = new HashMap<>();
        if (query.getBeginTime() != null && !query.getBeginTime().isEmpty()) {
            params.put("beginTime", query.getBeginTime());
        }
        if (query.getEndTime() != null && !query.getEndTime().isEmpty()) {
            params.put("endTime", query.getEndTime());
        }
        
        // 设置params属性
        try {
            java.lang.reflect.Field field = SysLoginLog.class.getDeclaredField("params");
            field.setAccessible(true);
            field.set(loginLog, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        // 查询总记录数
        List<SysLoginLog> list = loginLogMapper.selectLoginLogList(loginLog);
        long total = list.size();
        
        // 手动分页
        int pageNum = query.getPageNum();
        int pageSize = query.getPageSize();
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, list.size());
        
        if (fromIndex < list.size()) {
            list = list.subList(fromIndex, toIndex);
        } else {
            list.clear();
        }
        
        return new PageResult<>(list, total);
    }

    /**
     * 查询系统登录日志详细
     * 
     * @param id 登录ID
     * @return 登录日志对象
     */
    @Override
    public SysLoginLog selectLoginLogById(Long id) {
        // 如果Mapper层没有实现此方法，可以通过现有列表方法查询
        SysLoginLog query = new SysLoginLog();
        query.setId(id);
        List<SysLoginLog> logs = loginLogMapper.selectLoginLogList(query);
        return logs.isEmpty() ? null : logs.get(0);
    }
    
    /**
     * 批量删除系统登录日志
     * 
     * @param ids 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLoginLogByIds(Long[] ids) {
        return loginLogMapper.deleteLoginLogByIds(ids);
    }

    /**
     * 删除系统登录日志
     * 
     * @param id 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLoginLogById(Long id) {
        return loginLogMapper.deleteLoginLogById(id);
    }

    /**
     * 清空系统登录日志
     * 
     * @return 结果
     */
    @Override
    public int cleanLoginLog() {
        return loginLogMapper.cleanLoginLog();
    }
} 